﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Royalsoft.Common;

namespace NJL.WebUsers
{
    public partial class SearchList : BasePageVisitor
    {
        private readonly BLL.NJL_CS_SERVICE_TYPE bllServiceType = new BLL.NJL_CS_SERVICE_TYPE();
        private readonly BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        private readonly BLL.NJL_COMPANY_SERVICE bllService = new BLL.NJL_COMPANY_SERVICE();
        private readonly BLL.NJL_CS_TAG bllTag = new BLL.NJL_CS_TAG();
        private readonly BLL.NJL_NEWS bllNews = new BLL.NJL_NEWS();
        private readonly BLL.NJL_SYS_NEWS bllSysNews = new BLL.NJL_SYS_NEWS();
        private readonly BLL.NJL_LOCAL_COLOR bllLocalColor = new BLL.NJL_LOCAL_COLOR();
        private readonly BLL.NJL_ATTRACTION bllAttraction = new BLL.NJL_ATTRACTION();

        protected IList<Model.NJL_CS_TAG> LIST_TAG;
        protected IList<Model.NJL_NEWS> LIST_NEWS;
        protected IList<Model.NJL_COMPANY> LIST_COMPANY_NEAR;
        protected IList<Model.NJL_COMPANY> LIST_COMPANY_HOT_2;
        protected IList<Model.NJL_LOCAL_COLOR> LIST_LOCAL_COLOR;
        protected IList<Model.NJL_ATTRACTION> LIST_ATTRACTION;

        protected string SEARCH_WHERE = "";

        protected void Page_Load(object sender, EventArgs e)
        {

            #region 右侧数据
            if (WEBSITE_FOCUS_CITY_CODE != "")
            {
                #region 标签
                LIST_TAG = bllTag.GetModelList(15, " NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' and TAG_TYPE='5' and AREA_CODE='" + WEBSITE_FOCUS_CITY_CODE + "'", "SEARCH_COUNT desc,USED_COUNT DESC");
                #endregion

                #region 农家资讯
                rptNews.DataSource = bllSysNews.GetList(5, "NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' and  TYPE_CODE='19' and IS_PUBLISH=1 AND IS_INDEX=1 ", "PUBLISH_DATE_TIME desc");
                rptNews.DataBind();
                #endregion

                #region 周边
                LIST_COMPANY_NEAR = bllCompany.GetModelList(6, "NJL_COMPANY.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' AND NJL_COMPANY.AUDIT_STATE=1 AND (NJL_COMPANY.TYPE_CODE='2' or NJL_COMPANY.TYPE_CODE='1') ", "CREATE_DATE_TIME ASC");
                #endregion

                #region 热门度假山庄
                LIST_COMPANY_HOT_2 = bllCompany.GetModelList(6, "NJL_COMPANY.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' AND NJL_COMPANY.AUDIT_STATE=1 AND (NJL_COMPANY.TYPE_CODE='2') and NJL_COMPANY.AREA_CODE='" + WEBSITE_FOCUS_CITY_CODE + "'", "CREATE_DATE_TIME DESC");
                #endregion

                #region 热门景点
                LIST_ATTRACTION = bllAttraction.GetModelList(10, " NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' and AREA_CODE='" + WEBSITE_FOCUS_CITY_CODE + "' AND IS_PUBLISH=1 ", " ID");
                #endregion
            }
            else
            {
                #region 标签
                LIST_TAG = bllTag.GetModelList(15, " NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' and TAG_TYPE='5'","SEARCH_COUNT desc,USED_COUNT DESC");
                #endregion

                #region 农家资讯
                rptNews.DataSource = bllSysNews.GetList(5, "NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' and  TYPE_CODE='19' and IS_PUBLISH=1 AND IS_INDEX=1 ", "PUBLISH_DATE_TIME desc");
                rptNews.DataBind();
                #endregion

                #region 周边
                LIST_COMPANY_NEAR = bllCompany.GetModelList(6, "NJL_COMPANY.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' AND NJL_COMPANY.AUDIT_STATE=1 AND (NJL_COMPANY.TYPE_CODE='2' or NJL_COMPANY.TYPE_CODE='1') ", "CREATE_DATE_TIME ASC");
                #endregion

                #region 热门度假山庄
                LIST_COMPANY_HOT_2 = bllCompany.GetModelList(6, "NJL_COMPANY.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' AND NJL_COMPANY.AUDIT_STATE=1 AND (NJL_COMPANY.TYPE_CODE='2')", "CREATE_DATE_TIME DESC");
                #endregion

                #region 热门景点
                LIST_ATTRACTION = bllAttraction.GetModelList(10, " NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' AND IS_PUBLISH=1 ", " ID");
                #endregion
            }


            #endregion
              if (!IsPostBack)
              {
                  txtKeywords.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + ibtnSearch.ClientID + "').click();return false;}} else {return true}; ");
                  LoadData();
                  BindData();
              }         
        }

        private void LoadData()
        {
            cblServiceType.DataSource = bllServiceType.GetAllList();
            cblServiceType.DataValueField = "TYPE_CODE";
            cblServiceType.DataTextField = "TYPE_NAME";
            cblServiceType.DataBind();

            if ("" != WEBSITE_FOCUS_CITY_CODE)
            {
                SEARCH_WHERE = "NJL_COMPANY.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' AND NJL_COMPANY.AUDIT_STATE=1 AND (NJL_COMPANY.TYPE_CODE='2' or NJL_COMPANY.TYPE_CODE='1') and NJL_COMPANY.AREA_CODE='" + WEBSITE_FOCUS_CITY_CODE + "'";
            }
            else
            {
                SEARCH_WHERE = "NJL_COMPANY.NJL_SYS_BASE_ID='" + WEBSITE_CODE + "' AND NJL_COMPANY.AUDIT_STATE=1 AND (NJL_COMPANY.TYPE_CODE='2' or NJL_COMPANY.TYPE_CODE='1') ";
            }

            if (Request.QueryString["key"] != null)
            {
                string strKey = Server.HtmlDecode(Request.QueryString["key"]);
                SEARCH_WHERE = SEARCH_WHERE + " AND (NJL_COMPANY.COMPANY_NAME Like '%" + strKey + "%' OR NJL_COMPANY.ID IN (SELECT NJL_COMPANY_ID FROM NJL_COMPANY_SERVICE WHERE SERVICE_NAME Like '%" + strKey + "%' OR TAG like '%" + strKey + "%'))";
                txtKeywords.Text = strKey;
            }
            if (Request.QueryString["st"] != null && Request.QueryString["st"] != "")
            {
                SEARCH_WHERE = SEARCH_WHERE + " AND NJL_COMPANY.ID IN (SELECT NJL_COMPANY_ID FROM NJL_COMPANY_SERVICE WHERE TYPE_CODE IN (" + Request.QueryString["st"] + "))";

                string[] strsServiceType = Request.QueryString["st"].Split(new char[] { ',' });
                for (int i = 0; i < cblServiceType.Items.Count; i++)
                {
                    for (int j = 0; j < strsServiceType.Length; j++)
                    {
                        if (cblServiceType.Items[i].Value == strsServiceType[j].Trim())
                        {
                            cblServiceType.Items[i].Selected = true;
                        }
                    }
                }
            }

            
        }
        private void BindData()
        {
            DataSet ds = bllCompany.GetList(SEARCH_WHERE);
            if (ds.Tables[0].Rows.Count > 0)
            {
                pager.InnerHtml = Pager.GetPageNum(ds, rptHolidayValla, 5, new string[] { "st", "key","cc" });
            }
            else
            {
                pager.InnerHtml = "抱歉,没有您要查找的信息";
            }
            #region 标签搜索+1
            NJL.BLL.NJL_CS_TAG bllTag = new BLL.NJL_CS_TAG();
            NJL.Model.NJL_CS_TAG modelTag;
            ds = bllTag.GetList("TAG='" + Request.QueryString["key"] + "' and TAG_TYPE='5'");
            if (ds.Tables[0].Rows.Count > 0)
            {
                modelTag = bllTag.GetModel(ds.Tables[0].Rows[0][0].ToString());
                modelTag.SEARCH_COUNT += 1;
                if (bllTag.Update(modelTag))
                {
                }
            }
            #endregion
        }

        protected void ibtnSearch_Click(object sender, EventArgs e)
        {
            
            string strValue = "";
            for (int i = 0; i < cblServiceType.Items.Count; i++)
            {
                if (cblServiceType.Items[i].Selected)
                {
                    if (strValue == "")
                    {
                        strValue = cblServiceType.Items[i].Value;
                    }
                    else
                    {
                        strValue += "," + cblServiceType.Items[i].Value;
                    }
                }
            }

            string strKey = txtKeywords.Text.Trim();
            if (strKey == "试试输入你喜欢的活动，快速找到最好玩的！")
            {
                strKey = "";
            }

            if (strValue == "")
            {
                Response.Redirect("/SearchList.aspx?cc=" + WEBSITE_FOCUS_CITY_NAME_PINYIN + "&key=" + Server.UrlEncode(strKey), true);
            }
            else
            {
                Response.Redirect("/SearchList.aspx?cc=" + WEBSITE_FOCUS_CITY_NAME_PINYIN + "&st=" + strValue + "&key=" + Server.UrlEncode(strKey), true);
            }
        }

        protected void rptHolidayValla_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                string id = drv["ID"].ToString();
                Repeater rtp = e.Item.FindControl("rptService") as Repeater;
                string strWhere="NJL_COMPANY_ID='" + id + "'";
                if (Request.QueryString["st"] != null && Request.QueryString["st"] != "")
                {
                    strWhere += " AND TYPE_CODE IN (" + Request.QueryString["st"] + ")";
                }
                DataSet ds = bllService.GetList(4, strWhere, "CREATE_DATE_TIME desc");
                rtp.DataSource = ds;
                rtp.DataBind();
            }
        }

    }
}